package com.loveyukari.controller;


import com.loveyukari.common.Pager;
import com.loveyukari.common.exception.InternalException;
import com.loveyukari.model.Cosmetics;
import com.loveyukari.service.CosmeticsService;
import com.loveyukari.support.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("/admin/cosmetics")
@Controller
public class CosmeticsController {

    @Autowired
    private CosmeticsService cosmeticsService;

    @RequestMapping("/list")
    public String list(Model model, Pager pager,Integer status) {
        List<Cosmetics> list = cosmeticsService.getCosmeticsListByPage(pager,status);
        model.addAttribute("list", list);
        model.addAttribute("status", status == null ? -2 : status);
        return "cosmetics/list";
    }

    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String update(Integer id, Model model) {
        if (id == null) {
            return "redirect:/admin/cosmetics/list";
        }
        Cosmetics cosmetics = cosmeticsService.getCosmeticsDetail(id);
        if (cosmetics == null) {
            return "redirect:/admin/cosmetics/list";
        }
        model.addAttribute("cosmetics", cosmetics);
        return "cosmetics/edit";
    }


    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult update(Cosmetics cosmetics) {
        try {
            cosmeticsService.updateCosmetics(cosmetics);
            return new JsonResult();
        } catch (InternalException e) {
            return new JsonResult(1, e.getMessage());
        } catch (Exception e) {
            return new JsonResult(-1, "服务器异常");
        }
    }


    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(Model model) {
        return "cosmetics/add";
    }


    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult add(Cosmetics cosmetics) {
        try {
            cosmeticsService.addCosmetics(cosmetics);
            return new JsonResult();
        } catch (InternalException e) {
            return new JsonResult(1, e.getMessage());
        } catch (Exception e) {
            return new JsonResult(-1, "服务器异常");
        }
    }


    @RequestMapping("/updateAccount")
    @ResponseBody
    public JsonResult updateAccount(int id,String account){
        cosmeticsService.updateAccount(id,account);
        return new JsonResult();
    }

    @RequestMapping("/updateSuccess")
    @ResponseBody
    public JsonResult updateSuccess(int id){
        cosmeticsService.updateSuccess(id);
        return new JsonResult();
    }

    @RequestMapping("/close")
    @ResponseBody
    public JsonResult close(int id){
        cosmeticsService.close(id);
        return new JsonResult();
    }


}
